# Setup environment ----
library(data.table)
library(cowplot)
library(ggplot2)
library(ggridges)
library(extrafont)
library(Cairo)
source("R/functions.R")
suppressMessages(loadfonts())
theme_set(theme_minimal() + theme(
  plot.title = element_text(size = 14, hjust = .5),
  text = element_text(family = "CM Roman"),
  panel.border = element_rect(fill = NA, color = "gray70"),
  legend.position = "none"))
results_path <- "results/replication/"
load("data/data-2021-02-22.RData")

# Senate figure data ----
load(paste0(results_path, "senate_krls_counterfactuals.RData"))
available_seats <- senate_data[ , .(available_seats = .N), year]
senate_krls_counterfactuals <- rbindlist(senate_krls_counterfactuals)
senate_krls_counterfactuals <- merge(senate_krls_counterfactuals,
  unincluded_senate_seat_totals[, .(year, D, R)], by = "year")
gg_senate_min_max_data <- merge(
  senate_krls_counterfactuals[type_or_quantile == "0.95",
    .(boot, year, n_dems_max_dem = n_dems + D)],
  senate_krls_counterfactuals[type_or_quantile == "0.05",
    .(boot, year, n_dems_max_rep = n_dems + D)],
  by = c("boot", "year"))
gg_senate_min_max_data[, seat_swing := n_dems_max_dem - n_dems_max_rep]
gg_senate_min_max_data[, n_reps_max_dem := 100 - n_dems_max_dem]
gg_senate_min_max_data[, n_reps_max_rep := 100 - n_dems_max_rep]
gg_senate_zero_money_data <- merge(
  senate_krls_counterfactuals[type_or_quantile == "actual",
    .(boot, year, n_dems_actual = n_dems + D)],
  senate_krls_counterfactuals[type_or_quantile == "zero",
    .(boot, year, n_dems_zero = n_dems + D)],
  by = c("boot", "year"))
gg_senate_zero_money_data[, seat_swing := n_dems_actual - n_dems_zero]
gg_senate_zero_money_data[, n_reps_actual := 100 - n_dems_actual]
gg_senate_zero_money_data[, n_reps_zero := 100 - n_dems_zero]

# House figure data ----
load(paste0(results_path, "house_krls_counterfactuals.RData"))
house_counterfactuals <- rbindlist(house_krls_counterfactuals)
house_counterfactuals <- merge(house_counterfactuals,
  unincluded_house_seat_totals[, .(year, D, R)], by = "year")
gg_house_min_max_data <- merge(
  house_counterfactuals[type_or_quantile == "0.95",
    .(boot, year, n_dems_max_dem = n_dems + D)],
  house_counterfactuals[type_or_quantile == "0.05",
    .(boot, year, n_dems_max_rep = n_dems + D)],
  by = c("boot", "year"))
gg_house_min_max_data[, seat_swing := n_dems_max_dem - n_dems_max_rep]
gg_house_min_max_data[, n_reps_max_dem := 435 - n_dems_max_dem]
gg_house_min_max_data[, n_reps_max_rep := 435 - n_dems_max_rep]
gg_house_zero_money_data <- merge(
  house_counterfactuals[type_or_quantile == "actual",
    .(boot, year, n_dems_actual = n_dems + D)],
  house_counterfactuals[type_or_quantile == "zero",
    .(boot, year, n_dems_zero = n_dems + D)],
  by = c("boot", "year"))
gg_house_zero_money_data[, seat_swing := n_dems_actual - n_dems_zero]
gg_house_zero_money_data[, n_reps_actual := 435 - n_dems_actual]
gg_house_zero_money_data[, n_reps_zero := 435 - n_dems_zero]

# Make Figure 1 ----
load(paste0(results_path, "house_krls_results.RData"))
house_krls_results[, `:=`(
  group = factor(dplyr::case_when(
    dem_inc_lq_chal + dem_inc_hq_chal == 1 ~ "Dem. Incumbent",
    rep_inc_lq_chal + rep_inc_hq_chal == 1 ~ "Rep. Incumbent",
    TRUE ~ "All"
  ), levels = c("All",
    "Rep. Incumbent", "Dem. Incumbent"))
)]
fig_house_nonmonotonic <-
  ggplot(house_krls_results[bottom_tail == 0 & top_tail == 0],
    aes(dem_spend_adv, d_dem_spend_adv)) +
  geom_hline(yintercept = 0, lty = 3, color = "gray") +
  geom_point(alpha = .05, shape = 21, fill = "black", color = "white",
    stroke = 0) +
  geom_smooth(data = house_krls_results[bottom_tail == 0 & top_tail == 0 &
      group != "All"],
    aes(group = group, linetype = group),
    method = "loess", se = FALSE) +
  geom_smooth(method = "loess") +
  scale_linetype_manual(values = c(
    "All" = "solid",
    "Rep. Incumbent" = "dotted",
    "Dem. Incumbent" = "dashed"), drop = FALSE) +
  scale_x_continuous(breaks = seq(-1.5, 1.5, .5)) +
  scale_y_continuous(breaks = c(0, .03, .06),
    labels = c("0%", "3%", "6%")) +
  xlab(" ") + ylab("Marginal Effect of Spending\non Democratic Vote Share") +
  ggtitle("House") +
  coord_cartesian(expand = FALSE, xlim = c(-1.75, 1.75), ylim = c(-.015, .06)) +
  theme(legend.position = "bottom", legend.title = element_blank())

load(paste0(results_path, "senate_krls_results.RData"))
senate_krls_results[, `:=`(
  group = factor(dplyr::case_when(
    dem_incumbent == 1 ~ "Dem. Incumbent",
    dem_incumbent + open_seat == 0 ~ "Rep. Incumbent",
    TRUE ~ "All"
  ), levels = c("All",
    "Rep. Incumbent", "Dem. Incumbent"))
)]
fig_senate_nonmonotonic <-
  ggplot(senate_krls_results[bottom_tail == 0 & top_tail == 0],
    aes(dem_spend_adv, d_dem_spend_adv)) +
  geom_hline(yintercept = 0, lty = 3, color = "gray") +
  geom_point(alpha = .2, shape = 21, fill = "black", color = "white",
    stroke = 0) +
  geom_smooth(data = senate_krls_results[bottom_tail == 0 & top_tail == 0 &
      group != "All"], aes(group = group, linetype = group),
    method = "loess", se = FALSE) +
  geom_smooth(method = "loess") +
  scale_linetype_manual(values = c(
    "All" = "solid",
    "Rep. Incumbent" = "dotted",
    "Dem. Incumbent" = "dashed")) +
  scale_x_continuous(breaks = seq(-10, 10, 5)) +
  scale_y_continuous(breaks = c(0, .005, .01, .015),
    labels = c("0%", "0.5%", "1%", "1.5%")) +
  xlab(" ") + ylab(" ") + ggtitle("Senate") +
  coord_cartesian(expand = FALSE, xlim = c(-11, 11), ylim = c(-.015/4, .015))

h <- .125
h2 <- .03
fig_nonmonotonic <- cowplot::ggdraw() +
  cowplot::draw_plot(fig_house_nonmonotonic + theme(
    legend.position = "none",
    plot.title = element_text(size = 12),
    text = element_text(family = "CM Roman")),
    x = 0, y = h,  height = 1 - 2 * h, width = .5) +
  cowplot::draw_plot(fig_senate_nonmonotonic + theme(
    plot.title = element_text(size = 12),
    text = element_text(family = "CM Roman")),
    x = .5, y = h, height = 1 - 2 * h, width = .5 - h2) +
  cowplot::draw_label(
    paste0("Money Is Most Effective with Spending Near Parity"),
    size = 18, x = .5, y = 1 - h / 2,
    fontface = "bold",
    fontfamily = "CM Roman") +
  cowplot::draw_label(paste0(
    "Democratic Candidate's Spending Advantage\n(in millions of ",
    "inflation-adjusted US$)"),
    size = 12, x = .5, y = .75 * h+.025,
    fontfamily = "CM Roman") +
  cowplot::draw_plot(
    get_legend(fig_house_nonmonotonic + theme(
      plot.title = element_text(size = 12),
      text = element_text(family = "CM Roman"))),
    x = 0, y = 0, height = .05, width = 1)

pdf(paste0("figures/fig-nonmonotonic-", Sys.Date(), ".pdf"),
  width = 6.5, height = 4)
print(fig_nonmonotonic)
dev.off()
embed_fonts(paste0("figures/fig-nonmonotonic-", Sys.Date(), ".pdf"))

# Make Figure 2 ----
fig_house_min_max <- ggplot(
  gg_house_min_max_data,
  aes(y = year, group = year, fill = ..x..)) +
  geom_vline(xintercept = 218, lty = 3, color = "gray") +
  geom_density_ridges(
    aes(x = n_dems_max_dem),
    bandwidth = 4.35,
    scale = 1, size = 0.1, rel_min_height = 0.01,
    color = "gray92",
    #alpha = .25,
    fill = "#0276FD") +
  geom_density_ridges(
    aes(x = n_dems_max_rep),
    bandwidth = 4.35,
    scale = 1, size = 0.1, rel_min_height = 0.01,
    color = "gray92",
    alpha = .5,
    fill = "#ef2f4f") +
  scale_x_continuous(limits = c(50, 385), expand = c(0, 0),
    breaks = c(70, 145, 218, 290, 360)) +
  scale_y_reverse(breaks=seq(2016, 1980, -4), expand = c(0, 0.2)) +
  xlab("Democratic House Seats") +
  theme_minimal() +
  theme(panel.border = element_rect(color = "gray92", fill = NA),
    legend.position = "none",
    plot.title = element_text(hjust = .5)) +
  ggtitle("") +
  ylab("")

fig_senate_min_max <- ggplot(
  gg_senate_min_max_data,
  aes(y = year, group = year, fill = ..x..)) +
  geom_vline(xintercept = 50, lty = 3, color = "gray") +
  geom_density_ridges(
    aes(x = n_dems_max_dem),
    bandwidth = 1,
    scale = 1, size = 0.1, rel_min_height = 0.01,
    color = "gray92",
    #alpha = .25,
    fill = "#0276FD") +
  geom_density_ridges(
    aes(x = n_dems_max_rep),
    bandwidth = 1,
    scale = 1, size = 0.1, rel_min_height = 0.01,
    color = "gray92",
    alpha = .5,
    fill = "#ef2f4f") +
  scale_x_continuous(limits = c(25, 75), expand = c(0, 0),
    breaks = seq(30, 70, 10)) +
  scale_y_reverse(breaks=seq(2016, 1980, -4), expand = c(0, 0.2)) +
  xlab("Democratic Senate Seats") +
  theme_minimal() +
  theme(panel.border = element_rect(color = "gray92", fill = NA),
    legend.position = "none",
    plot.title = element_text(hjust = .5)) +
  ggtitle("") +
  ylab("")

h <- .15
h2 <- .03
fig_min_max <-
  cowplot::ggdraw() +
  cowplot::draw_plot(fig_house_min_max + ggtitle("House") +
      xlab("") + theme(
        plot.title = element_text(size = 12),
        text = element_text(family = "CM Roman")) +
      scale_y_reverse(breaks = seq(2016, 1980, -6),
        minor_breaks = seq(2016, 1980, -2), expand = c(0, .3)) +
      theme(panel.grid.minor = element_line(size = .1),
        panel.grid.major.y = element_line(size = .1)),
    x = 0, y = .35 * h,  height = 1 - 1 * h, width = .5) +
  cowplot::draw_plot(fig_senate_min_max + ggtitle("Senate") +
      xlab("") + theme(
        plot.title = element_text(size = 12),
        text = element_text(family = "CM Roman")) +
      scale_y_reverse(breaks = seq(2016, 1980, -6),
        minor_breaks = seq(2016, 1980, -2), expand = c(0, .3)) +
      theme(panel.grid.minor = element_line(size = .1),
        panel.grid.major.y = element_line(size = .1)),
    x = .5, y = .35 * h, height = 1 - 1 * h, width = .5 - h2) +
  cowplot::draw_label(
    paste0("Congressional Control Depends on Spending"),
    size = 18, x = .5, y = 1 - .4 * h, fontface = "bold",
    fontfamily = "CM Roman") +
  cowplot::draw_label(paste0(
    "Number of Seats Held by Democrats Under Hypothetical Spending Profiles"),
    size = 12, x = .5, y = .4 * h,
    fontfamily = "CM Roman")

pdf(paste0("figures/fig-min-max-", Sys.Date(), ".pdf"),
  width = 6.5, height = 4)
print(fig_min_max)
dev.off()
embed_fonts(paste0("figures/fig-min-max-", Sys.Date(), ".pdf"))

# Make Figure 3 ----
house_sig_years <- gg_house_zero_money_data[,
  .(year, V1 = jitter(n_dems_zero - n_dems_actual, factor = .001))
][, .(mean(V1), quantile(V1, .025), quantile(V1, .975)), year][
  which(sign(V2) == sign(V3) | V2 == 0 | V3 == 0), year]
senate_sig_years <- gg_senate_zero_money_data[,
  .(year, V1 = jitter(n_dems_zero - n_dems_actual, factor = .001))
][, .(mean(V1), quantile(V1, .025), quantile(V1, .975)), year][
  which(sign(V2) == sign(V3) | V2 == 0 | V3 == 0), year]
gg_house_zero_money_data[,
  sig := as.numeric(year %in% house_sig_years)]
gg_senate_zero_money_data[,
  sig := as.numeric(year %in% senate_sig_years)]
fig_house_zero_money <- ggplot(
  gg_house_zero_money_data,
  aes(y = year, group = year, fill = ..x..)) +
  geom_vline(xintercept = 0, lty = 3, color = "gray") +
  geom_density_ridges(
    aes(x = n_dems_zero - n_dems_actual, fill = factor(sig)),
    bandwidth = 2.35,
    scale = 1, size = 0.1, rel_min_height = 0.05,
    color = "gray92") +
  scale_y_reverse(breaks=seq(2016, 1980, -4), expand = c(0, 0.2)) +
  xlab("Change in Democratic House Seats") +
  theme_minimal() +
  theme(panel.border = element_rect(color = "gray92", fill = NA),
    legend.position = "none",
    plot.title = element_text(hjust = .5)) +
  ggtitle("") +
  scale_fill_manual(values = c("0" = "gray72", "1" = "gray42")) +
  ylab("")

fig_senate_zero_money <- ggplot(
  gg_senate_zero_money_data,
  aes(y = year, group = year, fill = ..x..)) +
  geom_vline(xintercept = 0, lty = 3, color = "gray") +
  geom_density_ridges(
    aes(x = n_dems_zero - n_dems_actual, fill = factor(sig)),
    bandwidth = 1,
    scale = 1, size = 0.1, rel_min_height = 0.05,
    color = "gray92") +
  scale_y_reverse(breaks=seq(2016, 1980, -4), expand = c(0, 0.2)) +
  xlab("Change in Democratic Senate Seats") +
  theme_minimal() +
  theme(panel.border = element_rect(color = "gray92", fill = NA),
    legend.position = "none",
    plot.title = element_text(hjust = .5)) +
  ggtitle("") +
  scale_fill_manual(values = c("0" = "gray72", "1" = "gray42")) +
  ylab("")

h <- .15
h2 <- .03
fig_zero_money <- cowplot::ggdraw() +
  cowplot::draw_plot(fig_house_zero_money + ggtitle("House") +
      xlab("") + theme(
        plot.title = element_text(size = 12),
        text = element_text(family = "CM Roman")) +
      scale_y_reverse(expand = c(0, .3)),
    x = 0, y = .35 * h,  height = 1 - 1.5 * h, width = .5) +
  cowplot::draw_plot(fig_senate_zero_money + ggtitle("Senate") +
      xlab("") + theme(
        plot.title = element_text(size = 12),
        text = element_text(family = "CM Roman")) +
      scale_y_reverse(expand = c(0, .3)),
    x = .5, y = .35 * h, height = 1 - 1.5 * h, width = .5 - h2) +
  cowplot::draw_label(
    paste0("The Effect of Removing Money on Democratic Seats"),
    size = 18, x = .5, y = 1 - .8 * h, fontface = "bold",
    fontfamily = "CM Roman") +
  cowplot::draw_label(paste0(
    "Additional Seats Held by Democrats under Zero Spending"),
    size = 12, x = .5, y = .25 * h,
    fontfamily = "CM Roman")

pdf(paste0("figures/fig-zero-money-", Sys.Date(), ".pdf"),
  width = 6.5, height = 4)
print(fig_zero_money)
dev.off()
embed_fonts(paste0("figures/fig-zero-money-", Sys.Date(), ".pdf"))

# Make Figure 4 ----
load(paste0(results_path, "house_analysis_data.RData"))
house_gap_data <- house_data[
  real_dem_expenditure_advantage_w_outside >= hou_tail_lims[1] &
    real_dem_expenditure_advantage_w_outside <= hou_tail_lims[2],
  .(gap = mean(real_dem_expenditure_advantage_w_outside[
    real_dem_expenditure_advantage_w_outside > 0]) -
      mean(real_dem_expenditure_advantage_w_outside[
        real_dem_expenditure_advantage_w_outside < 0])),
  year]

fig_house_dmr <-
  ggplot(house_krls_results[bottom_tail == 0 & top_tail == 0],
    aes(10^log_total_spending, d_dem_spend_adv)) +
  geom_hline(yintercept = 0, lty = 3, color = "gray") +
  geom_point(alpha = .05, shape = 21, fill = "black", color = "white",
    stroke = 0) +
  geom_smooth(method = "loess") +
  scale_x_log10(breaks = c(1e5, 1e6, 1e7)/1e6, labels = c("$100k", "$1M", "$10M")) +
  scale_y_continuous(breaks = c(0, .03, .06),
    labels = c("0%", "3%", "6%")) +
  xlab(" ") + ylab("Marginal Effect of Spending\non Democratic Vote Share") +
  ggtitle(" ") +
  coord_cartesian(expand = FALSE, xlim = c(1e5-1e3, 1e7+1e5)/1e6, ylim = c(-.015, .07)) +
  theme(legend.position = "bottom", legend.title = element_blank())

house_gap_data <- house_data[
  real_dem_expenditure_advantage_w_outside >= hou_tail_lims[1] &
    real_dem_expenditure_advantage_w_outside <= hou_tail_lims[2],
  .(total = mean(real_total_spending_w_outside),
    gap = mean(abs(real_dem_expenditure_advantage_w_outside))
  ), year]
fig_house_gap <- ggplot(house_gap_data, aes(year, gap)) +
  geom_point() +
  geom_smooth(method = "lm") +
  ylab("Mean Expenditure Advantage\n(millions of real US$)") +
  xlab("") +
  scale_y_continuous(limits = c(0, 1.1),
    breaks = c(0, .5, 1), labels = c(" $0M", "$0.5M", " $1M")) +
  coord_cartesian(xlim = c(1979, 2019), ylim = c(0, 1.1), expand = FALSE) +
  ggtitle("House")

fig_house_total <- ggplot(house_gap_data, aes(year, total)) +
  geom_point() +
  geom_smooth(method = "lm") +
  ylab("Mean Total Spending\n(millions of real US$)") +
  # ylab("Millions of Inflation-adjusted US$") +
  xlab(" ") +
  scale_y_continuous(limits = c(0, 4),
    breaks = c(0, 2, 4), labels = c("$0M", " $2M", " $4M")) +
  coord_cartesian(xlim = c(1979, 2019), expand = FALSE) +
  ggtitle(" ")

load(paste0(results_path, "senate_analysis_data.RData"))
senate_gap_data <- senate_data[
  real_dem_expenditure_advantage_w_outside >= sen_tail_lims[1] &
    real_dem_expenditure_advantage_w_outside <= sen_tail_lims[2],
  .(gap = mean(real_dem_expenditure_advantage_w_outside[
    real_dem_expenditure_advantage_w_outside > 0]) -
      mean(real_dem_expenditure_advantage_w_outside[
        real_dem_expenditure_advantage_w_outside < 0])),
  year]
fig_senate_dmr <-
  ggplot(senate_krls_results[bottom_tail == 0 & top_tail == 0],
    aes(10^log_total_spending, d_dem_spend_adv)) +
  geom_hline(yintercept = 0, lty = 3, color = "gray") +
  geom_point(alpha = .2, shape = 21, fill = "black", color = "white",
    stroke = 0) +
  geom_smooth(method = "loess") +
  scale_x_log10(breaks = c(1e6, 1e7, 1e8)/1e6, labels = c("$1M", "$10M", "$100M")) +
  scale_y_continuous(breaks = c(0, .005, .01, .015),
    labels = c("0%", "0.5%", "1%", "1.5%")) +
  xlab(" ") + ylab(" ") + ggtitle(" ") +
  coord_cartesian(expand = FALSE, xlim = c(1e6-1e4, 1e8+1e6)/1e6, ylim = c(-.015/(4.667), .015)) +
  theme(legend.position = "bottom", legend.title = element_blank())

senate_gap_data <- senate_data[
  real_dem_expenditure_advantage_w_outside >= sen_tail_lims[1] &
    real_dem_expenditure_advantage_w_outside <= sen_tail_lims[2],
  .(total = mean(real_total_spending_w_outside),
    gap = mean(abs(real_dem_expenditure_advantage_w_outside))
  ), year]
fig_senate_gap <- ggplot(senate_gap_data, aes(year, gap)) +
  geom_point() +
  geom_smooth(method = "lm") +
  ylab(" ") +
  xlab("") +
  scale_y_continuous(limits = c(0, 6.3),
    breaks = c(0, 3, 6), labels = c(" $0M", " $3M", " $6M")) +
  coord_cartesian(xlim = c(1979, 2019), expand = FALSE) +
  ggtitle("Senate")

fig_senate_total <- ggplot(senate_gap_data, aes(year, total)) +
  geom_point() +
  geom_smooth(method = "lm") +
  ylab(" ") +
  xlab("") +
  scale_y_continuous(limits = c(0, 33),
    breaks = c(0, 15, 30), labels = c("$0M", "$15M", "$30M")) +
  coord_cartesian(xlim = c(1979, 2019), expand = FALSE) +
  ggtitle(" ")

h <- .85 / 3
w <- .03
fig_gap_dmr <- cowplot::ggdraw() +
  cowplot::draw_plot(fig_house_gap,
    x = 0, y = .05 + 2 * h,  height = h, width = .5) +
  cowplot::draw_plot(fig_senate_gap,
    x = .5, y = .05 + 2 * h, height = h, width = .5 - w) +
  cowplot::draw_plot(fig_house_total,
    x = 0, y = .05 + 1 * h,  height = h, width = .5) +
  cowplot::draw_plot(fig_senate_total,
    x = .5, y = .05 + 1 * h, height = h, width = .5 - w) +
  cowplot::draw_plot(fig_house_dmr,
    x = 0, y = .05,  height = h, width = .5) +
  cowplot::draw_plot(fig_senate_dmr,
    x = .5, y = .05, height = h, width = .5 - w) +
  cowplot::draw_label(
    paste0(
      "Increasing Gaps and Decreasing Returns\nAttenuate Spending Effects"),
    size = 18, x = .5, y = .94, fontface = "bold",
    fontfamily = "CM Roman") +
  cowplot::draw_label(paste0(
    "Log of Total Spending"),
    size = 10, x = .5, y = .06,
    fontfamily = "CM Roman")

pdf(paste0("figures/fig-gap-dmr-", Sys.Date(), ".pdf"),
  width = 6.5, height = 9)
print(fig_gap_dmr)
dev.off()
embed_fonts(paste0("figures/fig-gap-dmr-", Sys.Date(), ".pdf"))


# Make Senate SVM figure data ----
load(paste0(results_path, "senate_analysis_data.RData"))
load(paste0(results_path, "senate_boot_svm_cond_ame.RData"))
load(paste0(results_path, "senate_svm_counterfactuals.RData"))
# make gg_sen_nonmon_data
gg_sen_nonmon_data <- data.table(
  dem_spend_adv = sen_X[sen_X[, "bottom_tail"] + sen_X[, "top_tail"] == 0, 1],
  d_dem_spend_adv_mean =
    apply(senate_boot_svm_cond_ame, 1 , mean, na.rm = TRUE),
  d_dem_spend_adv_q025 =
    apply(senate_boot_svm_cond_ame, 1 , quantile, probs = .025, na.rm = TRUE),
  d_dem_spend_adv_q975 =
    apply(senate_boot_svm_cond_ame, 1 , quantile, probs = .975, na.rm = TRUE))
# make gg_senate_min_max_data_svm
available_seats <- senate_data[ , .(available_seats = .N), year]
gg_senate_min_max_data_svm <- merge(
  senate_svm_counterfactuals[actual == 0 &
      dem_spend_adv == sen_tail_lims[2],
    .(n_dems_max_dem = sum(boot_y > .5)),
    .(boot, year)],
  senate_svm_counterfactuals[actual == 0 &
      dem_spend_adv == sen_tail_lims[1],
    .(n_dems_max_rep = sum(boot_y > .5)),
    .(boot, year)],
  by = c("boot", "year"))
gg_senate_min_max_data_svm[, seat_swing := n_dems_max_dem - n_dems_max_rep]
gg_senate_min_max_data_svm <- merge(gg_senate_min_max_data_svm,
  unincluded_senate_seat_totals[, .(year, D, R)], by = "year")
gg_senate_min_max_data_svm <- merge(gg_senate_min_max_data_svm,
  available_seats, by = "year")
gg_senate_min_max_data_svm[,
  n_reps_max_dem := available_seats - n_dems_max_dem]
gg_senate_min_max_data_svm[,
  n_reps_max_rep := available_seats - n_dems_max_rep]
# make gg_senate_zero_money_data_svm
available_seats <- senate_data[ , .(available_seats = .N), year]
gg_senate_zero_money_data_svm <- merge(
  senate_svm_counterfactuals[actual == 1,
    .(n_dems_actual = sum(boot_y > .5)),
    .(boot, year)],
  senate_svm_counterfactuals[actual == 0 & dem_spend_adv == 0,
    .(n_dems_zero = sum(boot_y > .5)),
    .(boot, year)],
  by = c("boot", "year"))
gg_senate_zero_money_data_svm[, seat_swing := n_dems_actual - n_dems_zero]
gg_senate_zero_money_data_svm <- merge(gg_senate_zero_money_data_svm,
  unincluded_senate_seat_totals[, .(year, D, R)], by = "year")
gg_senate_zero_money_data_svm <- merge(gg_senate_zero_money_data_svm,
  available_seats, by = "year")
gg_senate_zero_money_data_svm[,
  n_reps_actual := available_seats - n_dems_actual]
gg_senate_zero_money_data_svm[,
  n_reps_zero := available_seats - n_dems_zero]

# Make House SVM figure data ----
load(paste0(results_path, "house_analysis_data.RData"))
load(paste0(results_path, "house_boot_svm_cond_ame.RData"))
load(paste0(results_path, "house_svm_counterfactuals.RData"))
# make gg_hou_nonmon_data
gg_hou_nonmon_data <- data.table(
  dem_spend_adv = hou_X[hou_X[, "bottom_tail"] + hou_X[, "top_tail"] == 0, 1],
  d_dem_spend_adv_mean =
    apply(house_boot_svm_cond_ame, 1 , mean, na.rm = TRUE),
  d_dem_spend_adv_q025 =
    apply(house_boot_svm_cond_ame, 1 , quantile, probs = .025, na.rm = TRUE),
  d_dem_spend_adv_q975 =
    apply(house_boot_svm_cond_ame, 1 , quantile, probs = .975, na.rm = TRUE))
# make gg_house_min_max_data_svm
available_seats <- house_data[ , .(available_seats = .N), year]
gg_house_min_max_data_svm <- merge(
  house_svm_counterfactuals[actual == 0 &
      dem_spend_adv == hou_tail_lims[2],
    .(n_dems_max_dem = sum(boot_y > .5)),
    .(boot, year)],
  house_svm_counterfactuals[actual == 0 &
      dem_spend_adv == hou_tail_lims[1],
    .(n_dems_max_rep = sum(boot_y > .5)),
    .(boot, year)],
  by = c("boot", "year"))
gg_house_min_max_data_svm[, seat_swing := n_dems_max_dem - n_dems_max_rep]
gg_house_min_max_data_svm <- merge(gg_house_min_max_data_svm,
  unincluded_house_seat_totals[, .(year, D, R)], by = "year")
gg_house_min_max_data_svm <- merge(gg_house_min_max_data_svm,
  available_seats, by = "year")
gg_house_min_max_data_svm[,
  n_reps_max_dem := available_seats - n_dems_max_dem]
gg_house_min_max_data_svm[,
  n_reps_max_rep := available_seats - n_dems_max_rep]
# make gg_house_zero_money_data_svm
available_seats <- house_data[ , .(available_seats = .N), year]
gg_house_zero_money_data_svm <- merge(
  house_svm_counterfactuals[actual == 1,
    .(n_dems_actual = sum(boot_y > .5)),
    .(boot, year)],
  house_svm_counterfactuals[actual == 0 & dem_spend_adv == 0,
    .(n_dems_zero = sum(boot_y > .5)),
    .(boot, year)],
  by = c("boot", "year"))
gg_house_zero_money_data_svm[, seat_swing := n_dems_actual - n_dems_zero]
gg_house_zero_money_data_svm <- merge(gg_house_zero_money_data_svm,
  unincluded_house_seat_totals[, .(year, D, R)], by = "year")
gg_house_zero_money_data_svm <- merge(gg_house_zero_money_data_svm,
  available_seats, by = "year")
gg_house_zero_money_data_svm[,
  n_reps_actual := available_seats - n_dems_actual]
gg_house_zero_money_data_svm[,
  n_reps_zero := available_seats - n_dems_zero]

# Make SVM version of Figure 1 ----
fig_house_nonmonotonic_svm <-
  ggplot(gg_hou_nonmon_data, aes(dem_spend_adv, d_dem_spend_adv_mean)) +
  geom_hline(yintercept = 0, lty = 3, color = "gray") +
  geom_ribbon(aes(ymin = d_dem_spend_adv_q025, ymax = d_dem_spend_adv_q975),
    alpha = .35) +
  geom_line(color = "blue") +
  scale_x_continuous(breaks = seq(-1.5, 1.5, .5)) +
  scale_y_continuous(breaks = c(0, .03, .06, .09),
    labels = c("0%", "3%", "6%", "9%")) +
  xlab(" ") + ylab("Marginal Effect of Spending") + ggtitle("House") +
  coord_cartesian(expand = FALSE, xlim = c(-1.75, 1.75), ylim = c(-.015, .09))
fig_senate_nonmonotonic_svm <-
  ggplot(gg_sen_nonmon_data, aes(dem_spend_adv, d_dem_spend_adv_mean)) +
  geom_hline(yintercept = 0, lty = 3, color = "gray") +
  geom_ribbon(aes(ymin = d_dem_spend_adv_q025, ymax = d_dem_spend_adv_q975),
    alpha = .35) +
  geom_line(color = "blue") +
  scale_x_continuous(breaks = seq(-10, 10, 5)) +
  scale_y_continuous(breaks = c(0, .005, .01),
    labels = c("0%", "0.5%", "1%")) +
  xlab(" ") + ylab(" ") + ggtitle("Senate") +
  coord_cartesian(expand = FALSE, xlim = c(-11, 11), ylim = c(-.015/9, .01))
h <- .125
h2 <- .03
fig_nonmonotonic_svm <- cowplot::ggdraw() +
    cowplot::draw_plot(fig_house_nonmonotonic_svm + theme(
      plot.title = element_text(size = 12),
      text = element_text(family = "CM Roman")),
      x = 0, y = h,  height = 1 - 2 * h, width = .5) +
    cowplot::draw_plot(fig_senate_nonmonotonic_svm + theme(
      plot.title = element_text(size = 12),
      text = element_text(family = "CM Roman")),
      x = .5, y = h, height = 1 - 2 * h, width = .5 - h2) +
    cowplot::draw_label(
      paste0("Nonlinear Effect Replication with SVM"),
      size = 18, x = .5, y = 1 - h / 2, fontface = "bold",
      fontfamily = "CM Roman") +
    cowplot::draw_label(paste0(
      "Democratic Candidate's Spending Advantage\n(in millions of ",
      "inflation-adjusted US$)"),
      size = 12, x = .5, y = .75 * h,
      fontfamily = "CM Roman")
pdf(paste0("figures/fig-nonmonotonic-svm-", Sys.Date(), ".pdf"),
  width = 6.5, height = 4)
print(fig_nonmonotonic_svm)
dev.off()
embed_fonts(paste0("figures/fig-nonmonotonic-svm-", Sys.Date(), ".pdf"))

# Make SVM version of Figure 2 ----
fig_senate_min_max_svm <- ggplot(
  gg_senate_min_max_data_svm,
  aes(y = year, group = year, fill = ..x..)) +
  geom_vline(xintercept = 50, lty = 3, color = "gray") +
  geom_density_ridges(
    aes(x = D + n_dems_max_dem),
    bandwidth = 1,
    scale = 1, size = 0.1, rel_min_height = 0.01,
    color = "gray92",
    #alpha = .25,
    fill = "#0276FD") +
  geom_density_ridges(
    aes(x = D + n_dems_max_rep),
    bandwidth = 1,
    scale = 1, size = 0.1, rel_min_height = 0.01,
    color = "gray92",
    alpha = .5,
    fill = "#ef2f4f") +
  scale_x_continuous(limits = c(25, 75), expand = c(0, 0),
    breaks = seq(30, 70, 10)) +
  scale_y_reverse(breaks=seq(2016, 1980, -4), expand = c(0, 0.2)) +
  xlab("Democratic Senate Seats") +
  theme_minimal() +
  theme(panel.border = element_rect(color = "gray92", fill = NA),
    legend.position = "none",
    plot.title = element_text(hjust = .5)) +
  ggtitle("") +
  ylab("")

fig_house_min_max_svm <- ggplot(
  gg_house_min_max_data_svm,
  aes(y = year, group = year, fill = ..x..)) +
  geom_vline(xintercept = 218, lty = 3, color = "gray") +
  geom_density_ridges(
    aes(x = D + n_dems_max_dem),
    bandwidth = 4.35,
    scale = 1, size = 0.1, rel_min_height = 0.01,
    color = "gray92",
    #alpha = .25,
    fill = "#0276FD") +
  geom_density_ridges(
    aes(x = D + n_dems_max_rep),
    bandwidth = 4.35,
    scale = 1, size = 0.1, rel_min_height = 0.01,
    color = "gray92",
    alpha = .5,
    fill = "#ef2f4f") +
  scale_x_continuous(limits = c(0, 435), expand = c(0, 0),
    breaks = c(70, 145, 218, 290, 360)) +
  scale_y_reverse(breaks=seq(2016, 1980, -4), expand = c(0, 0.2)) +
  xlab("Democratic House Seats") +
  theme_minimal() +
  theme(panel.border = element_rect(color = "gray92", fill = NA),
    legend.position = "none",
    plot.title = element_text(hjust = .5)) +
  ggtitle("") +
  ylab("")

h <- .15
h2 <- .03
fig_min_max_svm <-
  cowplot::ggdraw() +
    cowplot::draw_plot(fig_house_min_max_svm + ggtitle("House") +
        xlab("") + theme(
          plot.title = element_text(size = 12),
          text = element_text(family = "CM Roman")) +
        scale_y_reverse(breaks = seq(2016, 1980, -6),
          minor_breaks = seq(2016, 1980, -2), expand = c(0, .3)) +
        theme(panel.grid.minor = element_line(size = .1),
          panel.grid.major.y = element_line(size = .1)),
      x = 0, y = .35 * h,  height = 1 - 1 * h, width = .5) +
    cowplot::draw_plot(fig_senate_min_max_svm + ggtitle("Senate") +
        xlab("") + theme(
          plot.title = element_text(size = 12),
          text = element_text(family = "CM Roman")) +
        scale_y_reverse(breaks = seq(2016, 1980, -6),
          minor_breaks = seq(2016, 1980, -2), expand = c(0, .3)) +
        theme(panel.grid.minor = element_line(size = .1),
          panel.grid.major.y = element_line(size = .1)),
      x = .5, y = .35 * h, height = 1 - 1 * h, width = .5 - h2) +
    cowplot::draw_label(
      paste0("Congressional Control Depends on Spending (SVM)"),
      size = 18, x = .5, y = 1 - .4 * h, fontface = "bold",
      fontfamily = "CM Roman") +
    cowplot::draw_label(paste0(
      "Number of Seats Held by Democrats Under Hypothetical Spending Profiles"),
      size = 12, x = .5, y = .4 * h,
      fontfamily = "CM Roman")

pdf(paste0("figures/fig-min-max-svm-", Sys.Date(), ".pdf"),
  width = 6.5, height = 4)
print(fig_min_max_svm)
dev.off()
embed_fonts(paste0("figures/fig-min-max-svm-", Sys.Date(), ".pdf"))

# Make SVM version of Figure 3 ----
senate_sig_years <- gg_senate_zero_money_data_svm[,
  .(year, V1 = jitter(n_dems_zero - n_dems_actual, factor = .001))
][, .(mean(V1), quantile(V1, .025), quantile(V1, .975)), year][
  which(sign(V2) == sign(V3) | V2 == 0 | V3 == 0), year]
gg_senate_zero_money_data_svm[,
  sig := as.numeric(year %in% senate_sig_years)]

fig_senate_zero_money_svm <- ggplot(
  gg_senate_zero_money_data_svm,
  aes(y = year, group = year, fill = ..x..)) +
  geom_vline(xintercept = 0, lty = 3, color = "gray") +
  geom_density_ridges(
    aes(x = n_dems_zero - n_dems_actual, fill = factor(sig)),
    bandwidth = 1,
    scale = 1, size = 0.1, rel_min_height = 0.05,
    color = "gray92") +
  scale_y_reverse(breaks=seq(2016, 1980, -4), expand = c(0, 0.2)) +
  xlab("Change in Democratic Senate Seats") +
  theme_minimal() +
  theme(panel.border = element_rect(color = "gray92", fill = NA),
    legend.position = "none",
    plot.title = element_text(hjust = .5)) +
  ggtitle("") +
  scale_fill_manual(values = c("0" = "gray72", "1" = "gray42")) +
  ylab("")

house_sig_years <- gg_house_zero_money_data_svm[,
  .(year, V1 = jitter(n_dems_zero - n_dems_actual, factor = .001))
][, .(mean(V1), quantile(V1, .025), quantile(V1, .975)), year][
  which(sign(V2) == sign(V3) | V2 == 0 | V3 == 0), year]
gg_house_zero_money_data_svm[,
  sig := as.numeric(year %in% house_sig_years)]

fig_house_zero_money_svm <- ggplot(
  gg_house_zero_money_data_svm,
  aes(y = year, group = year, fill = ..x..)) +
  geom_vline(xintercept = 0, lty = 3, color = "gray") +
  geom_density_ridges(
    aes(x = n_dems_zero - n_dems_actual, fill = factor(sig)),
    bandwidth = 4.35,
    scale = 1, size = 0.1, rel_min_height = 0.05,
    color = "gray92") +
  # scale_x_continuous(limits = c(50, 385), expand = c(0, 0),
  #   breaks = c(70, 145, 218, 290, 360)) +
  scale_y_reverse(breaks=seq(2016, 1980, -4), expand = c(0, 0.2)) +
  xlab("Change in Democratic House Seats") +
  theme_minimal() +
  theme(panel.border = element_rect(color = "gray92", fill = NA),
    legend.position = "none",
    plot.title = element_text(hjust = .5)) +
  ggtitle("") +
  scale_fill_manual(values = c("0" = "gray72", "1" = "gray42")) +
  ylab("")


h <- .15
h2 <- .03
fig_zero_money_svm<- cowplot::ggdraw() +
    cowplot::draw_plot(fig_house_zero_money_svm + ggtitle("House") +
        xlab("") + theme(
          plot.title = element_text(size = 12),
          text = element_text(family = "CM Roman")) +
        scale_y_reverse(expand = c(0, .3)),
      x = 0, y = .35 * h,  height = 1 - 1.5 * h, width = .5) +
    cowplot::draw_plot(fig_senate_zero_money_svm + ggtitle("Senate") +
        xlab("") + theme(
          plot.title = element_text(size = 12),
          text = element_text(family = "CM Roman")) +
        scale_y_reverse(expand = c(0, .3)),
      x = .5, y = .35 * h, height = 1 - 1.5 * h, width = .5 - h2) +
    cowplot::draw_label(
      paste0("The Effect of Removing Money (SVM)"),
      size = 18, x = .5, y = 1 - .8 * h, fontface = "bold",
      fontfamily = "CM Roman") +
    cowplot::draw_label(paste0(
      "Additional Seats Held by Democrats under Zero Spending"),
      size = 12, x = .5, y = .25 * h,
      fontfamily = "CM Roman")
pdf(paste0("figures/fig-zero-money-svm-", Sys.Date(), ".pdf"),
  width = 6.5, height = 4)
print(fig_zero_money_svm)
dev.off()

embed_fonts(paste0("figures/fig-zero-money-svm-", Sys.Date(), ".pdf"))

# Make Figure A1 (robustness of Figure 1) ----
load(paste0(results_path, "house_rf.RData"))
load(paste0(results_path, "house_svm.RData"))
load(paste0(results_path, "house_lasso.RData"))
load(paste0(results_path, "senate_rf.RData"))
load(paste0(results_path, "senate_svm.RData"))
load(paste0(results_path, "senate_lasso.RData"))
oos_data <- rbind(
  house_rf_data[incl == TRUE,
    .(dem_spend_adv, d_dem_spend_adv, Method = "RF", chamber = "House")],
  house_svm_data[incl == TRUE,
    .(dem_spend_adv, d_dem_spend_adv, Method = "SVM", chamber = "House")],
  house_lasso_data[incl == TRUE,
    .(dem_spend_adv, d_dem_spend_adv = round(d_dem_spend_adv, 4),
      Method = "LASSO", chamber = "House")],
  house_krls_results[bottom_tail+top_tail == 0,
    .(dem_spend_adv, d_dem_spend_adv, Method = "KRLS", chamber = "House")],
  senate_rf_data[incl == TRUE,
    .(dem_spend_adv, d_dem_spend_adv, Method = "RF", chamber = "Senate")],
  senate_svm_data[incl == TRUE,
    .(dem_spend_adv, d_dem_spend_adv, Method = "SVM", chamber = "Senate")],
  senate_lasso_data[incl == TRUE,
    .(dem_spend_adv,  d_dem_spend_adv = round(d_dem_spend_adv, 4),
      Method = "LASSO", chamber = "Senate")],
  senate_krls_results[bottom_tail+top_tail == 0,
    .(dem_spend_adv, d_dem_spend_adv, Method = "KRLS", chamber = "Senate")]
)
fig_oos <- ggplot(oos_data[Method != "LASSO"], aes(dem_spend_adv, d_dem_spend_adv,
  color = Method, linetype = Method)) +
  geom_smooth(se = FALSE, method = "loess", span = 1) +
  facet_wrap(~chamber, scales = "free") +
  xlab("Democratic Spending Advantage") +
  theme(legend.position = "bottom") +
  ylab("Estimated Average Marginal Effects") +
  scale_linetype_manual(values = c(
    "KRLS" = "solid", "SVM" = "dotted", "RF" = "longdash"
  ))
h <- .15
h2 <- .03
fig_oos <- cowplot::ggdraw() +
    cowplot::draw_plot(fig_oos + theme(
      plot.title = element_text(size = 12),
      text = element_text(family = "CM Roman")),
      x = 0, y = .35 * h,  height = 1 - 1.5 * h, width = 1) +
    cowplot::draw_label(
      paste0("Robustness of Nonlinear Effect Estimates"),
      size = 18, x = .5, y = 1 - .8 * h, fontface = "bold",
      fontfamily = "CM Roman")

pdf(paste0("figures/fig-oos-", Sys.Date(), ".pdf"),
  width = 6.5, height = 4)
print(fig_oos)
dev.off()
embed_fonts(paste0("figures/fig-oos-", Sys.Date(), ".pdf"))

# Smaller Counterfactuals Figure ----
load(paste0(results_path, "senate_krls_counterfactuals.RData"))
sc_summary <- rbindlist(senate_krls_counterfactuals)
sen_ggdat <- sc_summary[!type_or_quantile %in% c("zero", "actual"), .(
  q025 = quantile(n_dems, .025),
  q975 = quantile(n_dems, .975),
  med = median(n_dems)
), .(year, q = as.numeric(type_or_quantile))]
sen_ggdat <- merge(sen_ggdat, unincluded_senate_seat_totals[, .(year, D)],
  by = "year")
sen_ggdat[, `:=`(q025 = q025 + D, med = med + D, q975 = q975 + D)]
sen_ggdat[, group := ifelse(q <= .5, 1, 2)]
obs_n_dems_elected <- data.table(year = seq(1980, 2018, 2),
  obs_n_dems_elected = c(47, 46, 47, 55, 55, 56, 57, 48, 45, 45, 50, 49,
    45, 51, 59, 53, 55, 46, 48, 47))
sen_ggdat <- merge(sen_ggdat, obs_n_dems_elected, by = "year")
sen_ggdat[, real_dem_expenditure_advantage_w_outside := senate_data[,
  quantile(real_dem_expenditure_advantage_w_outside, q, na.rm = TRUE)]]

load(paste0(results_path, "house_krls_counterfactuals.RData"))
hc_summary <- rbindlist(house_krls_counterfactuals)
hou_ggdat <- hc_summary[!type_or_quantile %in% c("zero", "actual"), .(
  q025 = quantile(n_dems, .025), q975 = quantile(n_dems, .975),
  med = median(n_dems)), .(year, q = as.numeric(type_or_quantile))]
hou_ggdat <- merge(hou_ggdat, unincluded_house_seat_totals[, .(year, D)],
  by = "year")
hou_ggdat[, `:=`(q025 = q025 + D, med = med + D, q975 = q975 + D)]
hou_ggdat[, group := ifelse(q <= .5, 1, 2)]
obs_n_dems_elected <- data.table(year = seq(1980, 2018, 2),
  obs_n_dems_elected = rev(c(235, 194, 188, 201, 179, 257, 233, 203, 206, 213,
    212, 208, 205, 259, 268, 260, 258, 253, 269, 243)))
hou_ggdat <- merge(hou_ggdat, obs_n_dems_elected, by = "year")
hou_ggdat[, real_dem_expenditure_advantage_w_outside := house_data[,
  quantile(real_dem_expenditure_advantage_w_outside, q, na.rm = TRUE)]]

fig_senate_appendix_counterfactual <- ggplot(sen_ggdat,
  aes(real_dem_expenditure_advantage_w_outside))+
  facet_wrap(~ year) +
  geom_ribbon(aes(ymin = q025, ymax = q975, group = group), color = "gray",
    alpha = .5) +
  geom_hline(yintercept = 50, color = "red", linetype = "dotted") +
  geom_hline(aes(yintercept = obs_n_dems_elected), color = "blue",
    linetype = "dashed") +
  xlab("") +
  ylab("") +
  geom_line(aes(y = med, group = group)) +
  ggtitle("Senate") +
  geom_rug(data = senate_data[
    real_dem_expenditure_advantage_w_outside >= sen_tail_lims[1] &
      real_dem_expenditure_advantage_w_outside <= sen_tail_lims[2]],
    aes(x = real_dem_expenditure_advantage_w_outside),
    length = unit(.1, "npc")) +
  scale_x_continuous(breaks = c(-10, 0, 10)) +
  theme(axis.text =  element_text(size = 6))

fig_house_appendix_counterfactual <- ggplot(hou_ggdat,
  aes(real_dem_expenditure_advantage_w_outside))+
  facet_wrap(~year)+
  geom_ribbon(aes(ymin = q025, ymax = q975, group = group),
    color = "gray", alpha = .5) +
  geom_hline(yintercept = 218, color = "red", linetype = "dotted")+
  geom_hline(aes(yintercept = obs_n_dems_elected), color = "blue",
    linetype = "dashed")+
  geom_line(aes(y = med, group = group)) +
  xlab("") +
  ylab("Number of Seats Held by Democrats") +
  ggtitle("House") +
  geom_rug(data = house_data[
    real_dem_expenditure_advantage_w_outside >= hou_tail_lims[1] &
      real_dem_expenditure_advantage_w_outside <= hou_tail_lims[2]],
    aes(x = real_dem_expenditure_advantage_w_outside), alpha = .35,
    length = unit(.1, "npc")) +
  scale_y_continuous(breaks = c(218-100, 218, 218+100)) +
  theme(axis.text =  element_text(size = 6))

h <- .125
h2 <- .03
fig_appendix_counterfactual <- cowplot::ggdraw() +
    cowplot::draw_plot(fig_house_appendix_counterfactual + theme(
      legend.position = "none",
      plot.title = element_text(size = 12),
      text = element_text(family = "CM Roman")),
      x = 0, y = h,  height = 1 - 2 * h, width = .5) +
    cowplot::draw_plot(fig_senate_appendix_counterfactual + theme(
      plot.title = element_text(size = 12),
      text = element_text(family = "CM Roman")),
      x = .5, y = h, height = 1 - 2 * h, width = .5 - h2) +
    cowplot::draw_label(
      paste0("Congressional Control with Different Spending Advantages"),
      size = 15, x = .5, y = 1 - h / 2, fontface = "bold",
      fontfamily = "CM Roman") +
    cowplot::draw_label(paste0(
      "Democratic Candidate's Spending Advantage\n(in millions of ",
      "inflation-adjusted US$)"),
      size = 12, x = .5, y = .75 * h+.025,
      fontfamily = "CM Roman")

pdf(paste0("figures/fig-small-counterfactuals-", Sys.Date(), ".pdf"),
  width = 6.5, height = 8)
print(fig_appendix_counterfactual)
dev.off()
embed_fonts(paste0("figures/fig-small-counterfactuals-", Sys.Date(), ".pdf"))
